package com.mall.b2c.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mall.b2c.bean.DeliveryMethodBean;
import com.mall.b2c.dao.DeliveryMethodDao;
import com.mall.b2c.domain.delivery.DeliveryMethodDomain;
import com.mall.b2c.domain.member.ReceiverDomain;
import com.mall.b2c.service.DeliveryMethodService;
import com.mall.b2c.service.ReceiverService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 配送方式
 * @author lzx
 */
@Service("b2cDeliveryMethodService")
@Transactional
public class DeliveryMethodServiceImpl extends BaseServiceImpl<DeliveryMethodDomain, Long> implements DeliveryMethodService {
	@Resource private ReceiverService receiverService;

	@Resource
	public void setBaseDao(DeliveryMethodDao deliveryMethodDao) { 
		super.setBaseDao(deliveryMethodDao); 
	}

	@Override
	public DeliveryMethodDomain findDefault() {
		Finder finder = Finder.create("from DeliveryMethodDomain d where d.isDefault = :isDefault");
		finder.setParam("isDefault", true);
		return (DeliveryMethodDomain) finder.findUniqueByHQL();
	}

	@Override
	@Transactional(propagation= Propagation.REQUIRED)
	public void switchDefault(Long newId) {
		DeliveryMethodDomain newDeliveryMethod = this.find(newId);
		if(newDeliveryMethod == null){
			throw new NoticeException("该配送方式不存在!");
		}
		if(!newDeliveryMethod.getIsDefault()){
			DeliveryMethodDomain defaultDeliveryMethod = findDefault();
			if(defaultDeliveryMethod != null){
				defaultDeliveryMethod.setIsDefault(false);
				this.update(defaultDeliveryMethod);
			}
			newDeliveryMethod.setIsDefault(true);
		}else{
			newDeliveryMethod.setIsDefault(false);
		}
		this.update(newDeliveryMethod);
	}

	@Override
	public List<DeliveryMethodDomain> getDeliveryMethodList(Boolean isAvaliable) {
		Finder finder = Finder.create("from DeliveryMethodDomain bean where bean.isAvaliable = :isAvaliable order by bean.isDefault desc, bean.priority asc");
		finder.setParam("isAvaliable", isAvaliable);
		return finder.findHQLList();
	}

	@Override
	public BigDecimal calShipFee(Long receiverId, Long methodId, Long memberId, BigDecimal weight) {
		ReceiverDomain receiver = receiverService.getReceiver(receiverId, memberId);
		if(receiver == null){
			throw new NoticeException("收货地址不存在");
		}
		DeliveryMethodDomain method = find(methodId);
		if(method == null){
			throw new NoticeException("配送方式不存在");
		}
		return method.getShipFee(receiver.getProvinceId(), weight);
	}

	@Override
	public List<DeliveryMethodBean> getAvaliableDeliveries(Long receiverId,
			Long memberId, BigDecimal weight) {
		List<DeliveryMethodBean> methodes = new ArrayList<DeliveryMethodBean>();
		ReceiverDomain receiver = receiverService.getReceiver(receiverId, memberId);
		if(receiver == null){
			throw new NoticeException("收货地址不存在");
		}
		List<DeliveryMethodDomain> list = getDeliveryMethodList(true);
		for(DeliveryMethodDomain method : list){
			BigDecimal fee = method.getShipFee(receiver.getProvinceId(), weight);
			if(fee != null){
				DeliveryMethodBean bean = new DeliveryMethodBean();
				bean.setDescription(method.getDescription());
				bean.setFee(fee);
				bean.setIcon(method.getIcon());
				bean.setId(method.getId());
				bean.setName(method.getName());
				bean.setIsDefault(method.getIsDefault());
				methodes.add(bean);
			}
		}
		return methodes;
	}
}
